﻿using System;
using System.Windows.Input;

namespace MenusHunter.WinRT.Core.MVVM
{
    /// <summary>
    /// 
    /// </summary>
    public class ReplayCommand:ICommand
    {
        #region Fields

        private Action<object> _execute;
        private Predicate<object> _canExecute; 
        #endregion
        #region Construct
        /// <summary>
        /// 
        /// </summary>
        /// <param name="execute"></param>
        public  ReplayCommand(Action<object> execute ):this(execute,null)
        {
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="execute"></param>
        /// <param name="canExecute"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public ReplayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null) throw new ArgumentNullException("execute is null");
            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute(parameter);
        }

        /// <summary>
        /// 
        /// </summary>
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ReplayCommand<T>:ICommand
    {

        #region Fields

        private Action<T> _execute;
        private Predicate<object> _canExecute;
        #endregion
        #region Construct
        /// <summary>
        /// 
        /// </summary>
        /// <param name="execute"></param>
        public ReplayCommand(Action<T> execute)
            : this(execute, null)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="execute"></param>
        /// <param name="canExecute"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public ReplayCommand(Action<T> execute, Predicate<object> canExecute)
        {
            if (execute == null) throw new ArgumentNullException("execute is null");
            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute((T)parameter);
        }

        /// <summary>
        /// 
        /// </summary>
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }
    }
}
